<?php

namespace App\Http\Middleware;

use Closure;
use App\Repositories\Model\LogLoginRepository as LogLogin;
use Illuminate\Support\Facades\Auth;

class LoginLog
{
    /**
     * LogLoginRepository
     *
     * @var LogLogin
     */
    protected $log;

    /**
     * LoginLog constructor.
     * @param LogLogin $loglogin
     */
    public function __construct(LogLogin $loglogin)
    {
        $this->log = $loglogin;
    }

    /**
     * 记录登录日志
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $data = [
            'email' => $request->get('email', ''),
            'ip' => $request->getClientIp(),
            'is_login' => Auth::check() ? 1 : 0,
            'time' => time()
        ];
        $this->log->create($data);
        return $response;
    }
}
